﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using WebAppMatrix.Extensions;

namespace WebAppMatrix.Configuration
{
    public class MatrixSetting
    {
        #region 属性值

        public string AppName
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_AppName";
                string value = MatrixManager.CacheProvider.Fetch<string>(key, () =>
                {
                    string xpath = "Matrix/AppName";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return nodeValue;
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        public bool IsDebug
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_IsDebug";
                bool value = MatrixManager.CacheProvider.Fetch<bool>(key, () =>
                {
                    string xpath = "Matrix/IsDebug";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return Convert.ToBoolean(nodeValue);
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        public bool IsCache
        {
            get
            {
                //string key = "WebAppMatrix_MatrixSetting_IsCache";
                string xpath = "Matrix/IsCache";
                string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                return Convert.ToBoolean(nodeValue);
            }
        }

        public string CacheProvider
        {
            get
            {
                //string key = "WebAppMatrix_MatrixSetting_CacheProvider";
                string xpath = "Matrix/CacheProvider";
                string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                return nodeValue;
            }
        }

        /// <summary>
        /// 存放Sql配置文件的文件夹，Appsetting中配置节点为SqlFileFolder
        /// 默认为 /Config/Sql
        /// </summary>
        public string SqlFileFolder
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_SqlFileFolder";
                string value = MatrixManager.CacheProvider.Fetch<string>(key, () =>
                {
                    string xpath = "Matrix/SqlFileFolder";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return nodeValue;
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        /// <summary>
        /// SecurityValidationKey 用于身份验证是检测是否匹配
        /// </summary>
        public string SecurityValidationKey
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_SecurityValidationKey";
                string value = MatrixManager.CacheProvider.Fetch<string>(key, () =>
                {
                    string xpath = "Matrix/SecurityValidationKey";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return nodeValue;
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        /// <summary>
        /// ConnectionStringName 当前系统采用的连接字符串
        /// </summary>
        public string ConnectionStringName
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_ConnectionStringName";
                string value = MatrixManager.CacheProvider.Fetch<string>(key, () =>
                {
                    string xpath = "Matrix/ConnectionStringName";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return nodeValue;
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        /// <summary>
        /// SupperPassword 超级密码
        /// </summary>
        public string SupperPassword
        {
            get
            {
                string key = "WebAppMatrix_MatrixSetting_SupperPassword";
                string value = MatrixManager.CacheProvider.Fetch<string>(key, () =>
                {
                    string xpath = "Matrix/SupperPassword";
                    string nodeValue = IOExtensions.GetXmlNodeValue(xpath, MatrixSetting.MatrixSettingFile);

                    return nodeValue;
                });

                if (IsCache)
                    MatrixManager.CacheProvider.Insert(key, value);

                return value;
            }
        }

        #endregion

        #region 配置文件路径

        private static string matrixSettingFile;
        /// <summary>
        /// MatrixSettingFile配置文件的路径，Appsetting中配置节点为MatrixSettingFile
        /// 默认为：/Config/MatrixSettingFile.xml
        /// </summary>
        public static string MatrixSettingFile
        {
            get
            {
                if (matrixSettingFile.IsNullOrEmpty())    //初始化
                {
                    matrixSettingFile = Extensions.ConfigurationExtensions.GetAppSettingValue(
                        "MatrixSettingFile", "Config/MatrixSettingFile.xml");
                }

                matrixSettingFile = Extensions.IOExtensions.GenerateAbsolutePath(matrixSettingFile);

                return matrixSettingFile;
            }
        }

        #endregion
    }
}
